#include "stdafx.h"

#include "MultiScaleSubImageCollection.h"
#include "multiscalesubimage.h"

namespace DeepZoomImage
{
	MultiScaleSubImageCollection::MultiScaleSubImageCollection()
	{
		z_sorted = g_ptr_array_new ();
	}

	MultiScaleSubImageCollection::~MultiScaleSubImageCollection ()
	{
		g_ptr_array_free (z_sorted, true);
	}

	static int MultiScaleSubImageZIndexComparer (gconstpointer msisi1, gconstpointer msisi2)
	{
		int z1 = (*((MultiScaleSubImage**)msisi1))->GetZIndex ();
		int z2 = (*((MultiScaleSubImage**)msisi2))->GetZIndex ();

		return z1 - z2;
	}

	void MultiScaleSubImageCollection::ResortByZIndex ()
	{
		g_ptr_array_set_size (z_sorted, array->len);

		if (array->len == 0)
			return;
		
		for (guint i = 0; i < array->len; i++)
			z_sorted->pdata[i] = (MultiScaleSubImage *) array->pdata[i];

		if (array->len > 1)
			MergeSort (z_sorted->pdata, z_sorted->len, sizeof (void *), MultiScaleSubImageZIndexComparer);
	}

	bool MultiScaleSubImageCollection::Clear ()
	{
		g_ptr_array_set_size (z_sorted, 0);
		return true;  //DependencyObjectCollection::Clear ();
	}

	void *MultiScaleSubImageCollection::GetValueAt (int index)
	{
		if (index < 0 || (guint) index >= array->len)
			return NULL;

		return (void *) array->pdata[index];
	}

	bool MultiScaleSubImageCollection::SetValueAt (int index, void *value)
	{
		//MoonError error;
		//return SetValueAtWithError (index, value, &error);
		return true;
	}

	int MultiScaleSubImageCollection::Add (void *value)
	{
		//MoonError error;
		//return AddWithError (value, &error);
		return 0;
	}

	int MultiScaleSubImageCollection::Add (int value)
	{
		return Add (&value);
	}
}
